package com.sky.controller.user;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("userDishController")
@Api(tags = "菜品相关接口")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 根据分类id查询菜品
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> getByTypeId(Integer categoryId)
    {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //查询redis是否存在该分类
        String key = "dish_" + categoryId;
        List<DishVO> dishList = (List<DishVO>) valueOperations.get(key);
        if (dishList != null){
            return Result.success(dishList);
        }
        log.info("根据分类id查询菜品{} ",categoryId);
        dishList = dishService.getDishFlavorByTypeId(categoryId);
        valueOperations.set(key,dishList);
        return Result.success(dishList);
    }

}
